package String1;

import java.util.Random;
import java.util.Scanner;

public class String1_3 {
    public static void main(String[] args) {
        String username="asdwe";
        String userpw="123321";
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名");
            String name = scanner.next();
            System.out.println("请输入你的密码");
            String pw = scanner.next();
            String computercode=String1_3.yanzhengma();
            System.out.println("电脑产生的验证码是："+computercode);
            System.out.println("请输入电脑的验证码");
            String useryzm = scanner.next();
           while (!useryzm.equals(computercode)){
               Scanner scanner1 = new Scanner(System.in);
               System.out.println("验证码错误，请重新输入");
               computercode=String1_3.yanzhengma();
               System.out.println("电脑产生的验证码是："+computercode);
               useryzm=scanner.next();
           }

           //验证用户和密码是否正确
            if (name.equals(username)&& pw.equals(userpw)){
                System.out.println("用户登录成功");
            }else{
                if (i==2){
                    System.out.println("你的账号已经被锁定，请联系管理员");
                }else {
                    System.out.println("你的账户或密码错误你还剩下"+(2-i)+"次机会");
                }
            }

        }

    }//随机产生验证码
    public static String yanzhengma() {
        char[] code={'q','c','g','e','t','w','A','Q','Y','O','P','@','!','&'};
        Random random = new Random();
        String res="";
        for (int i = 0; i < 4; i++) {
            int index=random.nextInt(code.length);
            res+=code[index];
        }
        return res;
    }
}
